/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.guidance.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.guidance.entity.JpEvidence;
import com.jeeplus.modules.guidance.service.JpEvidenceService;

/**
 * 证据指引Controller
 * @author 姜盼
 * @version 2020-09-16
 */
@RestController
@RequestMapping(value = "/guidance/jpEvidence")
public class JpEvidenceController extends BaseController {

	@Autowired
	private JpEvidenceService jpEvidenceService;

	@ModelAttribute
	public JpEvidence get(@RequestParam(required=false) String id) {
		JpEvidence entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = jpEvidenceService.get(id);
		}
		if (entity == null){
			entity = new JpEvidence();
		}
		return entity;
	}


	/**
	 * 证据指引树表数据
	 */
	@GetMapping("list")
	public AjaxJson list(JpEvidence jpEvidence) {
		return AjaxJson.success().put("list", jpEvidenceService.findList(jpEvidence));
	}

	/**
	 * 根据Id获取证据指引数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(JpEvidence jpEvidence) {
		return AjaxJson.success().put("jpEvidence", jpEvidence);
	}

	/**
	 * 保存证据指引
	 */
	@PostMapping("save")
	public AjaxJson save(JpEvidence jpEvidence, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(jpEvidence);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		jpEvidenceService.save(jpEvidence);//保存
		return AjaxJson.success("保存证据指引成功");
	}

	/**
	 * 删除证据指引
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(JpEvidence jpEvidence) {
		jpEvidenceService.delete(jpEvidence);
		return AjaxJson.success("删除证据指引成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<JpEvidence> list = jpEvidenceService.findList(new JpEvidence());
		List rootTree = getJpEvidenceTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<JpEvidence> getJpEvidenceTree(List<JpEvidence> list, String extId) {
		List<JpEvidence> jpEvidences = Lists.newArrayList();
		List<JpEvidence> rootTrees = jpEvidenceService.getChildren("0");
		for (JpEvidence root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        jpEvidences.add(getChildOfTree(root, list, extId));
		    }
		}
		return jpEvidences;
	}

	private JpEvidence getChildOfTree(JpEvidence jpEvidence, List<JpEvidence> jpEvidenceList, String extId) {
		jpEvidence.setChildren(Lists.newArrayList());
		for (JpEvidence child : jpEvidenceList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(jpEvidence.getId())) {
		            jpEvidence.getChildren().add(getChildOfTree(child, jpEvidenceList, extId));
		        }
		    }
		}
		return jpEvidence;
	}



}